<?php

$info=json_decode($this->input->raw_input_stream,true);
if($this->goods_model->getbytbid($info["id"],$shop["id"],$sitename)) die("exist");

$color=array();
$colorimage=array();
$desc="";

if($info["desc"]){
    $desc.="<h2>Product Specifics</h2><p><ul class='props'>";
    $tags=[];
    foreach($info["desc"] as $item)
    {
        $row=explode(":",$item);
        if(!in_array(trim($row[0]),array("Package Contents","Approx Length")) && !in_array(trim($row[1]),array("#","/",",")))
        {
            $tags[]=trim($row[1]);
        }
        $desc.='<li>'.$row[0].' : '.$row[1].'</li>';
    }
    $desc.="</ul></p>";
}


$product=array(
    "catid"=>$shop["id"],
    "tbid"=>$info["id"],
    "name"=>$info["name"],
    "thumb"=>serialize($info["img"]),
    "source"=>$sitename,
    "source_url"=>$info["url"],
    "price"=>$info["price"],
    "sales"=>$info["sales"],
    "desc"=>$desc,
    "tags"=>implode(",",$tags)
);
$prop=array();

if($info["chart"])
{
    $product["desc"]=$product["desc"].$info["chart"];
}

if($info["colorinfo"])
{
    $icount=0;
    $color=array();
    $vc=[];
    $colorimage=array();
    foreach($info["colorinfo"] as $k=>$v)
    {
        $color[]=$v;
        $colorimage[]=$info["colorimg"][$k];
        if(isset($info["colorprice"]))
        {
            if($info["sizeinfo"]){
                foreach($info["sizeinfo"] as $s)
                {
                    $vc[$icount]=$info["colorprice"][$k];
                    $icount++;
                } 
            }
            else
            {
                $vc[$icount]=$info["colorprice"][$k];
                $icount++;
            }
        }
    }
    $prop["vv"][]=array("name"=>"Color","value"=>$color,"image"=>$colorimage);
    if($vc)
    {
        $prop["vc"]=$vc;
    }
}

if($info["sizeinfo"]){
    $size=array();
    foreach($info["sizeinfo"] as $s)
    {   
        $size[]=$s;
    }

    $prop["vv"][]=array("name"=>"Size","value"=>$size);
}
$product["option"]=serialize($prop);

if($this->goods_model->create($product)){
    die("ok");
}else{
    die("error");
}